import { Injectable } from '@nestjs/common';
import { calcAngle, calcLine } from 'src/common/math/ljMah';
import { PrismaService } from 'src/prisma/prisma.service';
import { CreateCalculateBetDto } from './dto/create-calculate-bet.dto';
import { UpdateCalculateBetDto } from './dto/update-calculate-bet.dto';

@Injectable()
export class CalculateBetService {
  constructor(private prisma: PrismaService) {}

  async createMany(args: Record<string, any>, planId) {
    const { id, w, H } = args;
    const bets = await this.prisma.bet.findMany({
      where: { lineId: +id },
      include: {
        leftTower: true,
        rightTower: true,
      },
    });
    // 计算线长
    const data = bets.map((bet) => {
      const t1 = bet.leftTower;
      const t2 = bet.rightTower;
      const l = bet.btSpan;
      const h = t2.altitude - t1.altitude;
      const angle = calcAngle(l, h);
      const line = calcLine(l, angle, w, H);
      return {
        betId: bet.id,
        line,
        planId,
      };
    });
    return await this.prisma.calcBet.createMany({ data });
  }

  async findAll(args: Record<string, any>) {
    const { id } = args;
    return await this.prisma.calcBet.findMany({
      where: { planId: +id },
      include: {
        plan: true,
        bet: {
          include: {
            leftTower: {
              include: {
                PointTower: true,
              },
            },
            rightTower: true,
          },
        },
      },
    });
  }
}
